package com.yzlwb.skt.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yzlwb.common.annotation.DataScope;
import com.yzlwb.common.core.domain.entity.SysDept;
import com.yzlwb.common.core.openapi.OpenApiConstant;
import com.yzlwb.common.utils.DateUtils;
import com.yzlwb.common.utils.http.HttpClientUtil;
import com.yzlwb.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yzlwb.skt.mapper.SktProjectMapper;
import com.yzlwb.skt.domain.SktProject;
import com.yzlwb.skt.service.ISktProjectService;
import org.springframework.util.CollectionUtils;

/**
 * 司空项目Service业务层处理
 * 
 * @author wfy
 * @date 2025-11-06
 */
@Service
public class SktProjectServiceImpl implements ISktProjectService 
{
    @Autowired
    private SktProjectMapper sktProjectMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询司空项目
     * 
     * @param id 司空项目主键
     * @return 司空项目
     */
    @Override
    public SktProject selectSktProjectById(Long id)
    {
        return sktProjectMapper.selectSktProjectById(id);
    }

    /**
     * 查询司空项目列表
     * 
     * @param sktProject 司空项目
     * @return 司空项目
     */
    @Override
    @DataScope(deptAlias = "p")
    public List<SktProject> selectSktProjectList(SktProject sktProject)
    {
        return sktProjectMapper.selectSktProjectList(sktProject);
    }

    /**
     * 新增司空项目
     * 
     * @param sktProject 司空项目
     * @return 结果
     */
    @Override
    public int insertSktProject(SktProject sktProject)
    {
        sktProject.setCreateTime(DateUtils.getNowDate());
        return sktProjectMapper.insertSktProject(sktProject);
    }

    /**
     * 修改司空项目
     * 
     * @param sktProject 司空项目
     * @return 结果
     */
    @Override
    public int updateSktProject(SktProject sktProject)
    {
        sktProject.setUpdateTime(DateUtils.getNowDate());
        return sktProjectMapper.updateSktProject(sktProject);
    }

    /**
     * 批量删除司空项目
     * 
     * @param ids 需要删除的司空项目主键
     * @return 结果
     */
    @Override
    public int deleteSktProjectByIds(Long[] ids)
    {
        return sktProjectMapper.deleteSktProjectByIds(ids);
    }

    /**
     * 删除司空项目信息
     * 
     * @param id 司空项目主键
     * @return 结果
     */
    @Override
    public int deleteSktProjectById(Long id)
    {
        return sktProjectMapper.deleteSktProjectById(id);
    }

    /**
     * 司空二项目信息更新
     * @param twoUserToken
     * @param twoName
     * @return
     */
    @Override
    public int updateDeptByTwoUserTokenAndTwoName(Long deptId) {
        JSONArray jsonArray = getSkProjectInfoList(deptMapper.selectDeptById(deptId).getTwoUserToken());
        List<JSONObject> jsonObjectList = jsonArray.toJavaList(JSONObject.class);

        SktProject sktProject= new SktProject();
        sktProject.setDeptId(deptId);
        List<SktProject> sktProjectList = sktProjectMapper.selectSktProjectList(sktProject);
        sktProjectMapper.deleteSktProjectByDeptId(deptId);
        for (SktProject skt:sktProjectList ){
            String projectUuid = skt.getTwoProjectUuid();
            for(JSONObject js:jsonObjectList ){
                if(projectUuid.equals(js.getString("uuid"))){

                    SktProject updateProject=new SktProject();
                    updateProject.setDeptId(deptId);
                    updateProject.setTwoName(js.getString("name"));
                    updateProject.setTwoIntroduction(js.getString("introduction"));
                    updateProject.setTwoOrgUuid(js.getString("org_uuid"));
                    updateProject.setTwoProjectUuid(js.getString("uuid"));
                    updateProject.setTwoUpdateAt(new Date(js.getLong("updated_at")));
                    updateProject.setTwoCreatedAt(new Date(js.getLong("created_at")));
                    updateProject.setTwoProjectLatitude(js.getJSONObject("project_work_center_point").getFloat("latitude")+"");
                    updateProject.setTwoProjectLongitude(js.getJSONObject("project_work_center_point").getFloat("longitude")+"");
                    updateProject.setCreateTime(new Date(js.getLong("created_at")));
                    updateProject.setUpdateTime(new Date(js.getLong("updated_at")));
                    sktProjectMapper.insertSktProject(updateProject);
                }
            }
        }
        return 1;
    }

    @Override
    public JSONArray getSkProjectInfoList(String twoUserToken) {

        Map<String,Object> headerMap = new HashMap<String,Object>();
        headerMap.put("X-User-Token",twoUserToken);
        Map<String, Object> paramMap = new HashMap<String,Object>();
        //paramMap.put("p",twoName);
        paramMap.put("sort_column","created_at" );
        paramMap.put("sort_type","DESC" );
        String resultStr = HttpClientUtil.doGet(OpenApiConstant.projectListUrl,paramMap,headerMap);
        JSONObject resultjson = JSONObject.parseObject(resultStr);
        Integer code = resultjson.getInteger("code");
        if(0==code) {
            return resultjson.getJSONObject("data").getJSONArray("list");
        }
        return null;
    }

    @Override
    public int deptAssociationSkProject(Long deptId,String twoUserToken, String twoNames) {

        sktProjectMapper.deleteSktProjectByDeptId(deptId);
        String[] twoNameList = twoNames.split("@");
        for (String twoName:twoNameList){
            Map<String,Object> headerMap = new HashMap<String,Object>();
            headerMap.put("X-User-Token",twoUserToken);
            Map<String, Object> paramMap = new HashMap<String,Object>();
            paramMap.put("p",twoName);
            paramMap.put("sort_column","created_at" );
            paramMap.put("sort_type","DESC" );
            String resultStr = HttpClientUtil.doGet(OpenApiConstant.projectListUrl,paramMap,headerMap);
            JSONObject resultjson = JSONObject.parseObject(resultStr);
            Integer code = resultjson.getInteger("code");
            if(0==code){
                JSONArray projectArraylist = resultjson.getJSONObject("data").getJSONArray("list");
                for (int i=0;i<projectArraylist.size();i++){
                    JSONObject project = projectArraylist.getJSONObject(i);
                    if(twoName.equals(project.getString("name"))){
                        SktProject sktProject=new SktProject();
                        sktProject.setDeptId(deptId);
                        sktProject.setTwoName(twoName);
                        sktProject.setTwoIntroduction(project.getString("introduction"));
                        sktProject.setTwoOrgUuid(project.getString("org_uuid"));
                        sktProject.setTwoProjectUuid(project.getString("uuid"));
                        sktProject.setTwoUpdateAt(new Date(project.getLong("updated_at")));
                        sktProject.setTwoCreatedAt(new Date(project.getLong("created_at")));
                        sktProject.setTwoProjectLatitude(project.getJSONObject("project_work_center_point").getFloat("latitude")+"");
                        sktProject.setTwoProjectLongitude(project.getJSONObject("project_work_center_point").getFloat("longitude")+"");
                        sktProjectMapper.insertSktProject(sktProject);
                    }
                }

            }
        }
        //更新部门表
        SysDept dept = new SysDept();
        dept.setDeptId(deptId);
        dept.setTwoUserToken(twoUserToken);
        return deptMapper.updateDept(dept);
    }

    @Override
    public List<JSONObject> getSkDevice(String twoUserToken,String twoProjectUuid) {
        Map<String,Object> headerMap = new HashMap<String,Object>();
        headerMap.put("X-User-Token",twoUserToken);
        headerMap.put("X-Project-Uuid",twoProjectUuid);
        String resultStr = HttpClientUtil.doGet(OpenApiConstant.projectDeviceList,headerMap);
        JSONObject resultjson = JSONObject.parseObject(resultStr);
        Integer code = resultjson.getInteger("code");
        if(0==code) {
            JSONArray  jsonArray= resultjson.getJSONObject("data").getJSONArray("list");
            if(jsonArray==null) return null;

            return jsonArray.toJavaList(JSONObject.class);
        }


        return null;
    }
}
